package com.xudow.app.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;

import com.xudow.app.R;
import com.xudow.app.util.PrefUtils;

import java.util.ArrayList;

/**
 * GuideActivity
 *
 * @author lukecheng
 */
public class GuideActivity extends Activity {

    private ViewPager viewPager;
    private ArrayList<View> pageViews;

    private LayoutInflater inflater;


    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_guide);

        viewPager = (ViewPager) findViewById(R.id.pages);

        inflater = getLayoutInflater();

        pageViews = new ArrayList<View>();
        pageViews.add(createView(0));
        pageViews.add(createView(1));
        pageViews.add(createView(2));
        pageViews.add(createView(3));

        viewPager.setAdapter(new GuidePageAdapter());
    }

    private View createView(int index) {
        View view = inflater.inflate(R.layout.item_guide, null);

        int imageRes = R.mipmap.guide0;

        switch (index) {
            case 0:
                imageRes = R.mipmap.guide0;
                break;
            case 1:
                imageRes = R.mipmap.guide1;
                break;
            case 2:
                imageRes = R.mipmap.guide2;
                break;
            case 3:
                imageRes = R.mipmap.guide3;
                view.findViewById(R.id.end_button).setVisibility(View.VISIBLE);
                break;
            default:
                break;
        }

        view.setBackgroundResource(imageRes);

        return view;
    }


    public void onEndButtonClick(View view) {
        PrefUtils.removeFirstLaunch(this);

        startActivity(new Intent(this, SplashActivity.class));
        finish();
    }

    class GuidePageAdapter extends PagerAdapter {

        @Override
        public int getCount() {
            return pageViews.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public void destroyItem(View v, int position, Object arg2) {
            ((ViewPager) v).removeView(pageViews.get(position));
        }

        @Override
        public Object instantiateItem(View v, int position) {
            ((ViewPager) v).addView(pageViews.get(position));

            return pageViews.get(position);
        }

        @Override
        public int getItemPosition(Object object) {
            return super.getItemPosition(object);
        }

        @Override
        public void restoreState(Parcelable arg0, ClassLoader arg1) {

        }

        @Override
        public Parcelable saveState() {
            return null;
        }
    }

}
